Date.prototype.Format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1, // 月份
        "d+": this.getDate(), // 日
        "h+": this.getHours(), // 小时
        "m+": this.getMinutes(), // 分
        "s+": this.getSeconds(), // 秒
        "q+": Math.floor((this.getMonth() + 3) / 3),
        // 季度
        "S": this.getMilliseconds() // 毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
};

function getAppUploadFileName(filenameSuffix) {
    var now = new Date();
    var nowFormat = now.Format('yyyy/MM/dd/hhmmssS' + filenameSuffix);
    return "upload/manage/" + nowFormat;
}

function uploadWithSDK(file_id, val_id, img_id, token, putExtra, config, domain) {
    $("#" + file_id).unbind("change").bind("change", function () {
            var file = this.files[0];

            if (file) {
            	var filename = file.name;
            	var index = filename.lastIndexOf('.');
            	var filenameSuffix = filename.substr(index);
            	
                // var key = file.name;
                var key = getAppUploadFileName(filenameSuffix);

                var error = function (err) {
                	alert('上传失败！');
                    console.log(err);
                };

                var complete = function (res) {
                    if (res.key && res.key.match(/\.(jpg|jpeg|png|gif|xls|xlsx)$/)) {
                        var url = domain + '/' + res.key;
                        $("#" + val_id).val(url);
                        if (img_id !== null && img_id !== undefined || img_id !== '') {
                            $("#" + img_id).attr('src',url);
                        }
                    }
                };

                var next = function (response) {
                    // var chunks = response.chunks || [];
                    // var total = response.total;
                };

                var subObject = {
                    next: next,
                    error: error,
                    complete: complete
                };
                // 调用sdk上传接口获得相应的observable，控制上传和暂停
                var observable = qiniu.upload(file, key, token, putExtra, config);
                var subscription = observable.subscribe(subObject);
                // subscription.unsubscribe();
            }
        }
    )
}

function uploadWithSDKByDom(fileDom, valDom, imgDom, token, putExtra, config, domain) {
	fileDom.unbind("change").bind("change", function () {
            var file = this.files[0];
            console.log(this.files);
            console.log(file);

            if (file) {
            	var filename = file.name;
            	var index = filename.lastIndexOf('.');
            	var filenameSuffix = filename.substr(index);
            	
                // var key = file.name;
                var key = getAppUploadFileName(filenameSuffix);

                var error = function (err) {
                	alert('上传失败！');
                    console.log(err);
                };

                var complete = function (res) {
                    if (res.key && res.key.match(/\.(jpg|jpeg|png|gif|xls|xlsx)$/)) {
                        var url = domain + '/' + res.key;
                        console.log(url);
                        valDom.val(url);
                        imgDom.attr('src', url);
                    }
                };

                var next = function (response) {
                    // var chunks = response.chunks || [];
                    // var total = response.total;
                };

                var subObject = {
                    next: next,
                    error: error,
                    complete: complete
                };
                // 调用sdk上传接口获得相应的observable，控制上传和暂停
                var observable = qiniu.upload(file, key, token, putExtra, config);
                var subscription = observable.subscribe(subObject);
                // subscription.unsubscribe();
            }
        }
    )
}
